#pragma once

#include "IWebsocketServer.h"
#include <sstream>
#include "CommonDef.h"
#include <map>

class WWebsocketServer;
struct WebsocketCbMsg
{
    WWebsocketServer *wsServer = nullptr;
    bool sslFlag = false;
};

class WWebsocketServer: public IWebsocketServer
{
public:
    WWebsocketServer();
    virtual ~WWebsocketServer();
    virtual bool startWsServer(int port);
    virtual bool startWssServer(int port, string certPath, string keyPath);
    virtual void setWsMsgCbFun(WsCbFun fun);
    virtual void setWsConnectCbFun(WsCbFun fun);
    virtual void setWsCloseCbFun(WsCbFun fun);
    virtual bool run();
    virtual bool stop();
    virtual bool sendMsg(shared_ptr<WsRequestMsg> wsRequestMsg, const string &msg);
private:
    mg_mgr *_mgr = nullptr;
    volatile int _wsPort = -1;
    volatile int _wssPort = -1;
    string _certPath = "";
    string _keyPath = "";
    WsCbFun _wsMsgCbFun = nullptr;
    WsCbFun _wsConnectCbFun = nullptr;
    WsCbFun _wsCloseCbFun = nullptr;
    WebsocketCbMsg _wsCbMsg;
    WebsocketCbMsg _wssCbMsg;

    void recvWsRequestMsg(struct mg_connection *conn, int msgType, void *msgData, void *cbData);

    static void recvWsRequestMsgCb(struct mg_connection *conn, int msgType, void *msgData, void *cbData);
};

